package com.fusion.core.comm.rpc;

import com.fusion.core.comm.Connection;
import com.fusion.core.comm.Packet;
import com.fusion.core.comm.RPC;
import com.fusion.core.comm.T;
import com.fusion.core.comm.upnp.ReverseConnection;

import java.io.IOException;

/**
 * @author SwethaShivakumar 
 * Project: fusion
 * Date: Mar 8, 2009
 * URL: http://fusionwiki.co.cc
 * Mail Me At: shiva@fusionproject.co.cc
 */
public class ConnectToMe extends RPC {
    private int connectionId;

    public ConnectToMe() {
    }

    public ConnectToMe(int connectionId) {
        this.connectionId = connectionId;
    }

    public void execute(Packet data) throws IOException {
        connectionId = data.readInt();
        if(T.t) T.debug(con.getRemoteFriend()+" want to rev connect with connectionId "+connectionId+". Lets do it.");

        ReverseConnection c = new ReverseConnection(core.getNetworkManager(), Connection.Direction.OUT, con.getRemoteUserGUID(), connectionId);
        manager.getNetMan().connect(con.getRemoteUserGUID(), c);
    }

    public Packet serializeTo(Packet p) {
        p.writeInt(connectionId);
        return p;
    }
}
